package com.ailot.cloud.upms.api.controller;

import com.ailot.cloud.base.common.dto.Result;
import com.ailot.cloud.base.common.exception.BizException;
import com.ailot.cloud.base.log.annotation.Oplog;
import com.ailot.cloud.base.security.utils.SecurityUtil;
import com.ailot.cloud.upms.api.service.SysUserService;
import com.ailot.cloud.upms.common.dto.JwtUserDTO;
import com.ailot.cloud.upms.common.dto.SysUserDTO;
import com.ailot.cloud.upms.common.entity.SysUser;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class SysUserController {

    private final SysUserService sysUserService;

    /**
     * 分页查询用户
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('sys:user:page')")
    public IPage<?> pageUser(Page<SysUser> page, SysUserDTO sysUserDTO) {
        return sysUserService.pageUser(page, sysUserDTO);
    }

    @PostMapping
    @PreAuthorize("hasAuthority('sys:user:add')")
    public Boolean addUser(@RequestBody SysUserDTO sysUserDTO) {
        return sysUserService.addUser(sysUserDTO);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:edit')")
    public Boolean updateUser(@Valid @RequestBody SysUserDTO sysUserDTO) {
        return sysUserService.updateUser(sysUserDTO);
    }

    @Oplog("删除用户")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:user:del')")
    public Boolean delUser(@PathVariable String id) {
        return sysUserService.removeById(id);
    }

    @GetMapping
    @PreAuthorize("hasAuthority('sys:user:get')")
    public JwtUserDTO info() {
        String username = SecurityUtil.getUsername();
        SysUser user = sysUserService.getOne(Wrappers.<SysUser>query()
                .lambda().eq(SysUser::getUsername, username));
        if (user == null) {
            throw new BizException("获取当前用户信息失败");
        }
        return sysUserService.getUserDetail(user);
    }

    /**
     * 修改用户密码
     */
    @Oplog("修改密码")
    @PutMapping("/password")
    @PreAuthorize("hasAuthority('sys:user:password')")
    public Result<?> editPassword(@Valid @RequestBody SysUserDTO sysUserDTO) {
        SysUser sysUser = new SysUser();
        sysUser.setId(sysUserDTO.getId());
        sysUser.setPassword(new BCryptPasswordEncoder().encode(sysUserDTO.getPassword()));
        sysUserService.updateById(sysUser);
        return Result.success();
    }

    /**
     * 获取指定用户全部信息
     *
     * @return 用户信息
     */
    @GetMapping("/getInfo/{username}")
    public JwtUserDTO getInfo(@PathVariable("username") String username) {
        SysUser sysUser = sysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, username));
        if (sysUser == null) {
            throw new BizException(String.format("用户信息为空 %s", username));
        }
        return sysUserService.getUserDetail(sysUser);
    }
}
